package com.cnbbx.cnbbx_web.controller;

import com.cnbbx.cnbbx_web.domain.UserInfo;
import com.cnbbx.cnbbx_web.service.UserInfoService;
import com.cnbbx.cnbbx_web.utils.ControllerUtil;
import com.cnbbx.cnbbx_web.vo.JsonReturn;
import com.cnbbx.mybatis.PageData;
import com.cnbbx.mybatis.query.builder.DeleteBuilder;
import com.cnbbx.mybatis.query.builder.InsertBuilder;
import com.cnbbx.mybatis.query.builder.QueryBuilder;
import com.cnbbx.mybatis.query.builder.UpdateBuilder;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@Api(value = "用户表控制器", tags = {"用户表控制器"})
@RequestMapping(value = "/v1/UserInfoController")
@Slf4j
public class UserInfoController<T> extends ControllerUtil {
    @Autowired
    UserInfoService userinfoService;

    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 403, message = "没有权限")
    })
    @ApiOperation(value = "查询多条数据", notes = "分页查询", httpMethod = "GET")
    @RequestMapping("/ListAll/{page}/{size}")
    public JsonReturn<PageData<UserInfo>> ListAll(
            @ApiParam(required = true, example = "1") @PathVariable(value = "page") Long page,
            @ApiParam(required = true, example = "10") @PathVariable(value = "size") Long size) {
        QueryBuilder queryBuilder = QueryBuilder.build(UserInfo.class).size(size).page(page - 1);
        PageData<UserInfo> listAll = userinfoService.page(queryBuilder);
        return JsonReturn(listAll, "没有数据");
    }

    @ApiOperation(value = "新增一条数据", notes = "新增", httpMethod = "POST")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 403, message = "没有权限")
    })
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public JsonReturn<UserInfo> addOne(@RequestBody UserInfo userInfo) {
        InsertBuilder insertBuilder = InsertBuilder.build(UserInfo.class).set(userInfo);
        int insert = 0;
        try {
            insert = userinfoService.insert(insertBuilder);
        } catch (Throwable e) {
            log.error("addOne新增失败：", e.getCause());
        }
        return JsonReturn(insert == 0 ? null : insert, "新增失败");
    }

    @ApiOperation(value = "查询一条记录", notes = "按ID查询", httpMethod = "GET")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 403, message = "没有权限")
    })
    @RequestMapping(value = "/getOne/{uId}", method = RequestMethod.GET)
    public JsonReturn<UserInfo> getOne(@ApiParam(required = true, example = "1") @PathVariable(value = "uId") String uId) {
        QueryBuilder queryBuilder = QueryBuilder.build(UserInfo.class).eq("uId", uId);
        UserInfo userInfo = userinfoService.one(queryBuilder);
        return JsonReturn(userInfo, "查询失败");
    }

    @ApiOperation(value = "删除一条记录", notes = "按ID删除", httpMethod = "GET")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 403, message = "没有权限")
    })
    @RequestMapping(value = "/DelOne/{uId}", method = RequestMethod.GET)
    public JsonReturn<UserInfo> DelOne(@ApiParam(required = true, example = "1") @PathVariable(value = "uId") String uId) {
        DeleteBuilder queryBuilder = DeleteBuilder.build(UserInfo.class).eq("uId", uId);
        int delete = userinfoService.delete(queryBuilder);
        return JsonReturn(delete == 0 ? null : delete, "删除失败");
    }

    @ApiOperation(value = "修改一条记录", notes = "按ID修改", httpMethod = "POST")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "操作成功"),
            @ApiResponse(code = 400, message = "错误请求"),
            @ApiResponse(code = 403, message = "没有权限")
    })
    @RequestMapping(value = "/EditOne", method = RequestMethod.POST)
    public JsonReturn<UserInfo> EditOne(@RequestBody UserInfo userInfo) {
        UpdateBuilder queryBuilder = UpdateBuilder.build(UserInfo.class).set(userInfo);
        int edit = userinfoService.update(queryBuilder);
        return JsonReturn(edit, "修改失败");
    }
}